package com.john.market.activities;

import java.util.UUID;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.john.market.R;
import com.john.market.classes.Product;
import com.john.market.classes.ProductAdapter;
import com.john.market.classes.UpdatableListActivity;
import com.john.market.constants.BundleKeys;
import com.john.market.constants.ProductType;
import com.john.market.interfaces.ISeller;
import com.john.market.interfaces.IStock;

/**
 * Activity that showing products for sale,
 * where user can add or update products
 * 
 */
public class StockActivity extends UpdatableListActivity implements IStock {
	// ID of the FormActivity
	private final int ACTIVITY_FORM = 0;

	// Seller object
	private ISeller mSeller = null;
	
	// Listener for the "Edit" button 
	private OnClickListener mOnEditClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// Getting product ID 
			UUID id = (UUID)view.getTag();
			int pos = getProductPos(id);
			
			// Getting product
			Product product = mProductList.get(pos);
			
			// Setting request bundle
			Bundle bundle = new Bundle();
			bundle.putBoolean(BundleKeys.KEY_FOR_EDIT, true);
			bundle.putString(BundleKeys.KEY_ID, product.getId().toString());
			bundle.putString(BundleKeys.KEY_TITLE, product.getTitle());
			bundle.putInt(BundleKeys.KEY_TYPE, product.getType().ordinal());
			bundle.putString(BundleKeys.KEY_AMOUNT, "" + product.getAmount());
			bundle.putString(BundleKeys.KEY_PRICE, "" + product.getPrice());
			
			// Creating intent for activity
			Intent intent = new Intent(StockActivity.this, FormActivity.class);
			intent.putExtras(bundle);
			
			// Start form activity
			startActivityForResult(intent, ACTIVITY_FORM);
			
		}
		
	};
	
	// Listener for the "Add" button
	private OnClickListener mOnAddClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// Setting request bundle
			Bundle bundle = new Bundle();
			bundle.putBoolean(BundleKeys.KEY_FOR_EDIT, false);
			
			// Creating intent for activity
			Intent intent = new Intent(StockActivity.this, FormActivity.class);
			intent.putExtras(bundle);
			
			// Start activity
			startActivityForResult(intent, ACTIVITY_FORM);
			
		}
		
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stock);
        
        // Creating adapter for the ListView
        this.mAdapter = new ProductAdapter(this, R.layout.product, this.mProductList, "Edit", false);
        this.mAdapter.setOnClickListener(this.mOnEditClickListener);
        
        // Set listeners for "Add" button
        Button btn = (Button)findViewById(R.id.request);
        btn.setOnClickListener(this.mOnAddClickListener);
        
        // Setting the list adapter
        setListAdapter(this.mAdapter);
        
	}
	
	@Override
	public void setSeller(ISeller seller) {
		this.mSeller = seller;
		
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) return;
		
		// Getting received bundle
		super.onActivityResult(requestCode, resultCode, data);
		Bundle extras = data.getExtras();
		
		// Check if result is RESUKT_OK, not RESULT_CANCELLED
		if (resultCode != RESULT_OK) return;

		// Selecting activity by its ID
		switch(requestCode) {
			case ACTIVITY_FORM: {
				// Getting mode
				boolean isEdited = extras.getBoolean(BundleKeys.KEY_FOR_EDIT);
				
				// Getting ID of the product
				UUID id = UUID.fromString(extras.getString(BundleKeys.KEY_ID));
				
				// Getting title of the product
				String title = extras.getString(BundleKeys.KEY_TITLE);

				// Getting type of the product
				int ordinal = extras.getInt(BundleKeys.KEY_TYPE);
				ProductType type = ProductType.values()[ordinal];
				
				// Getting amount of the product
				int amount = extras.getInt(BundleKeys.KEY_AMOUNT);
				
				// Remove the product if its amount less or equal zero,
				// or add or update it otherwise
				if (amount <= 0) {
					if (isEdited) {
						this.mSeller.removeProduct(id);
					}
				}
				else {
					// Getting price of the product
					float price = extras.getFloat(BundleKeys.KEY_PRICE);
				
					// Creating a new product with received data
					Product product = new Product(title, type, amount, price);
					product.setId(id);
					
					// Add or update the product depending on the mode
					if (isEdited) {
						this.mSeller.updateProduct(product);
					}
					else {
						this.mSeller.addProduct(product);
					}
				}
				
				break;
			}
		    
			default: {
				break;
			}
			
		}
		
    }
	
}
